﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFLib;
using WPFLib.Win;

namespace _30让ComboBox控件添加图标
{
    public class CComboBoxView : INotifyPropertyChanged
    {
        /// <summary>
        /// 此事件继承于接口INotifyPropertyChanged
        /// 必须实现
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        ImageSource _igSource;
        //Image _igSource;
        /// <summary>
        /// 图标资源
        /// </summary>
        public ImageSource IgSource
        //public Image IgSource
        {
            get => _igSource;
            set
            {
                _igSource = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IgSource"));
            }
        }

        string _fileName;
        /// <summary>
        /// 文本说明
        /// </summary>
        public string FileName
        {
            get => _fileName;
            set
            {
                _fileName = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FileName"));
            }
        }
    }

    public class CComboBoxViewList : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        List<CComboBoxView> _cComboBoxViews = new List<CComboBoxView>();

        /// <summary>
        /// 列表
        /// </summary>
        public List<CComboBoxView> CComboBoxViews
        {
            get => _cComboBoxViews;
            set
            {
                _cComboBoxViews = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CComboBoxViews"));
            }
        }
    }


    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "Resource\\DevImage\\";
        CComboBoxViewList cComboBoxViewList = new CComboBoxViewList();

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DirectoryInfo folder = new DirectoryInfo(path);
            
            ImageSource imageSource = null;

            foreach (FileInfo file in folder.GetFiles("*.png"))
            {
                imageSource = new BitmapImage(new Uri(path + file.Name, UriKind.Absolute));
                cComboBoxViewList.CComboBoxViews.Add(new CComboBoxView()
                {
                    IgSource = imageSource,
                    FileName = file.Name
                });
            }
            cmbImage.DataContext = cComboBoxViewList;
        }

        private void CmbImage_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            

        }

        private void CmbImage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox comboBox = (ComboBox)sender;
            CComboBoxView cbv = (CComboBoxView)comboBox.SelectedItem;
            if (cbv == null) return;

            Debug.Print("IgSource:" + cbv.IgSource.ToString());
            Debug.Print("FileName:" + cbv.FileName.ToString());
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            AddDevWindow adw = new AddDevWindow();
            adw.ShowDialog();
        }

        private void Btn1_Click(object sender, RoutedEventArgs e)
        {
            WPFWindows ww = new WPFWindows();
            ww.ShowDialog();
        }
    }
}
